﻿/*!
* jQuery Upload 
*
* Copyright 2010, Dufei 
* 
*/
(function ($) {
    if ($.uploadFile) return false; //防止JS重复加载
    $.fn.uploadFile = function (options) {
        var arrSuccess = [];
        this.each(function () {
            if (!$.nodeName(this, 'INPUT')) return;
            if (options === false)//卸载
            {
                if (this.uploadFile) {
                    this.uploadFile.remove();
                    this.uploadFile = null;
                }
            }
            else//初始化
            {
                if (!this.uploadFile) {
                    var tOptions = /({.*})/.exec($(this).attr('class'));
                    if (tOptions) {
                        try { tOptions = eval('(' + tOptions[1] + ')'); } catch (ex) { };
                        options = $.extend({}, tOptions, options);
                    }
                    var upload = new $.uploadFile(this, options);
                    if (upload.init()) {
                        this.uploadFile = upload;
                        arrSuccess.push(upload);
                    }
                    else upload = null;
                }
            }
        });
        if (arrSuccess.length == 0) arrSuccess = false;
        if (arrSuccess.length == 1) arrSuccess = arrSuccess[0];
        return arrSuccess;
    }

    var _arealist = null;

    $.uploadFile = function (input, options) {
        var _this = this;
        var _nextObj = null;

        if (options.count == null)
            options.count = 1;

        if (options.width == null)
            options.width = 300;

        if (options.tempfolder == null)
            options.tempfolder = "/uploadfiles/";

        if (options.photofolder == null)
            options.photofolder = "/uploadfiles/photo/";

        if (options.key == null)
            options.key = '';

        this.init = function () {
            $(input).hide();

            _this.showValue($(input).val());

            return true;
        }

        this.showForm = function () {
            _nextObj = $(input).next('div');
            if (_nextObj.size() == 0 || _nextObj.eq(0).attr('class') != 'uploadfileContainer') {
                $(input).after('<div class="uploadfileContainer"></div>');

                _nextObj = $(input).next('.uploadfileContainer').eq(0);
            }
            else {
                _nextObj = $(input).next('.uploadfileContainer').eq(0);

                return;
            }

            for (var i = 0; i < options.count; i++) {
                _nextObj.append('<div class="uploadfileOne"><div class="uploadfileInfo"></div><iframe name="uploadfileForm" src="about:blank" frameborder="0" scrolling="no" style="width:' + options.width + 'px;height:25px;"></iframe></div>');
                _nextObj.find('iframe').eq(i).bind('load', function () {
                    var _value = $(this).contents().find('body').html();
                    if (_value.indexOf('{') == 0) {
                        _this.success(_value);
                    }
                });
            }

            setTimeout(function () {
                _nextObj.find('iframe').each(function (i) {
                    _this.showOneForm(i);
                });
            }, 1);
        }

        this.showOneForm = function (i) {
            var _objFrame = _nextObj.find('iframe').eq(i);
            _objFrame.contents().find('head').append('<style>body{margin:0px;padding:0px;}input{border:solid 1px #C0C0C0;}</style>');
            _objFrame.contents().find('body').html('<form action="/Scripts/upload/upload.ashx?num=' + i + '&key=' + options.key + '" method="post" enctype="multipart/form-data"><input type="file" name="upfile" rel = "' + i + '" style="width:95%" /></form>');
            _objFrame.contents().find('body > form > input').bind('change', function () {
                _this.postFile($(this).attr('rel'));
                $(this).get(0).form.submit();
            });
        }

        this.showValue = function (value) {
            var arrFile = null;
            if (value != '') {
                arrFile = value.split("|");
                options.count = arrFile.length;
            }
            _this.showForm();

            if (arrFile == null || arrFile.length == 0)
                return;

            $.each(arrFile, function (i) {
                _this.showOneValue(arrFile[i], i);
            });
        }

        this.showOneValue = function (value, i) {
            if (value != '') {
                _nextObj.find('.uploadfileInfo').eq(i).html('<span>' + value + '</span>　[<a rel="' + i + '" name="reupload">重新上传</a>]');
                _nextObj.find('iframe').eq(i).hide();
                if ($("[class='preview']").length > 0) {
                    $("[class='preview']").hide();
                }
                if (_nextObj.find('.uploadfileOne').eq(i).find('img[class=preview]').size() == 0) {
                    _nextObj.find('.uploadfileOne').eq(i).prepend('<img class="preview" id="preview" src="" onload="if($(this).width() > 200){$(this).width(200);}" />');
                }

                var _objImg = _nextObj.find('.uploadfileOne').eq(i).find('img[class=preview]');

                var _flag = $(input).attr('flag');
                var _img = '';
                if (_flag == '1')
                    _img = options.photofolder + _nextObj.find('.uploadfileInfo').eq(i).find('span').html();
                else
                    _img = options.tempfolder + _nextObj.find('.uploadfileInfo').eq(i).find('span').html();

                _objImg.attr('src', _img).show();

                _nextObj.find('.uploadfileInfo').eq(i).find('[name=reupload]').bind('click', function () {
                    _nextObj.find('.uploadfileOne').eq(i).find('img').remove();
                    _nextObj.find('.uploadfileInfo').eq(i).html('').hide();
                    _this.showOneForm(i);
                    _nextObj.find('iframe').eq(i).show();
                    $(input).attr('flag', '0');

                    var value = $(input).val();
                    var arrFile = value.split("|");
                    value = '';
                    $.each(arrFile, function (n) {
                        if (value != '' || n > 0)
                            value += '|';
                        if (n != i) {
                            value += arrFile[n];
                        }
                    });
                    $(input).val(value);
                });
            }
        }

        this.postFile = function (num) {
            _nextObj.find('.uploadfileInfo').eq(num).html('<img src="/Scripts/upload/images/loading.gif" align="middle" /> 正在上传……');
            _nextObj.find('.uploadfileInfo').eq(num).show();
            _nextObj.find('iframe').eq(num).hide();
        }

        this.success = function (msg) {
            msg = eval("(" + msg + ")");
            if (msg.Result == 0) {
                _nextObj.find('.uploadfileInfo').eq(msg.Index).html('').hide();
                _this.showOneForm(msg.Index);
                _nextObj.find('iframe').eq(msg.Index).show();
                alert(msg.Msg);
            }

            var _value = $(input).val();

            var _count = _nextObj.find('.uploadfileOne').size();
            if (_value == '') {
                for (var i = 0; i < _count - 1; i++)
                    _value += '|';
            }
            var _arr = _value.split("|");
            _arr[msg.Index] = msg.FileName;

            _value = '';
            $.each(_arr, function (i) {
                if (_value != '')
                    _value += '|';
                _value += _arr[i];
            });
            $(input).val(_value);

            _this.showOneValue(msg.FileName, msg.Index);
        }
    }
})(jQuery);